<template>  
  <div class="system-equipInfo-edit">
    <!-- 添加或修改设备信息表;对话框 -->
    <el-dialog v-model="isShowDialog" width="800px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-equipInfo-edit .el-dialog', '.system-equipInfo-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'设备信息表;'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">        
        <el-form-item label="客户编号" prop="customerId">
          <el-select v-model="formData.customerId" placeholder="请选择客户编号" clearable>
            <el-option v-for="item in customerList" :key="item.customerId" :label="`${item.customerName}(${item.customerId})`" :value="item.customerId" />
          </el-select>
        </el-form-item>   
        
        <el-form-item label="设备编号" prop="equipNum">
          <el-alert title="设备编号添加成功后将不可修改" type="warning" :closable="false"/>
          <el-input style="height: auto;" v-model="formData.equipNum" placeholder="请输入设备编号" :disabled="formData.id?true:false"  />
          
        </el-form-item>                
        <el-form-item v-if="formData.id" label="设备激活状态" prop="equipActiveStatus">
          <el-select :style="{width:'150px'}" v-model="formData.equipActiveStatus" placeholder="请选择设备激活状态" clearable>
            <el-option v-for="dict in equip_active_status" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>        
        <el-form-item label="设备名称" prop="equipName">
          <el-input v-model="formData.equipName" placeholder="请输入设备名称" />
        </el-form-item>        
        <el-form-item label="设备mac地址" prop="equipMac">
          <el-input v-model="formData.equipMac" placeholder="请输入设备mac地址" />
        </el-form-item>        
        <el-form-item label="设备IP地址" prop="equipIp">
          <el-input v-model="formData.equipIp" placeholder="请输入设备IP地址" />
        </el-form-item>        
        <el-form-item label="设备通信端口" prop="equipPort">
          <el-input v-model="formData.equipPort" placeholder="请输入设备通信端口" />
        </el-form-item>          
        <el-form-item label="协议类型" prop="protocolType">
          <el-select v-model="formData.protocolType" placeholder="请选择协议类型" >
            <el-option v-for="dict in equip_protocol_type" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>        
        <el-form-item label="加密秘钥" prop="encryptKey">
          <el-input v-model="formData.encryptKey" placeholder="请输入加密秘钥" />
        </el-form-item>  
        <CitySelector :required="true" v-model:province="formData.equipProvince" v-model:city="formData.equipCity"
          v-model:area="formData.equipCounty" />             
        <el-form-item label="设备位置" prop="equipPosition">
          <el-input v-model="formData.equipPosition" placeholder="请输入设备位置" />
        </el-form-item>        
        <el-form-item label="描述信息" prop="equipDesc">
          <el-input v-model="formData.equipDesc" placeholder="请输入描述信息" />
        </el-form-item>            
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance, watch } from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import CitySelector from '/@/components/CitySelector/index.vue'
import {
    listEquipInfo,
    getEquipInfo,
    delEquipInfo,
    addEquipInfo,
    updateEquipInfo  
} from "../../api/equip";
import {
  listCustomerInfo,
} from "../../api/customer";
import lodash from 'lodash'
export default defineComponent({
  name:"ApiV1SystemEquipInfoEdit",  
  components:{
    CitySelector
  },
  setup(props,{emit}) {    
    const {proxy} = getCurrentInstance()
    const formRef = ref(null);
    const menuRef = ref();    
    const state = reactive({
      loading:false,
      isShowDialog: false,
      customerList: [],
      formData: {        
        id: undefined,        
        equipNum: undefined,           
        equipActiveStatus: 'waiting' ,        
        equipName: undefined,        
        equipMac: undefined,        
        equipIp: undefined,        
        equipPort: undefined,        
        protocolType: undefined,        
        encryptKey: undefined,        
        customerId: undefined,        
        equipProvince: undefined,        
        equipCity: undefined,        
        equipCounty: undefined,        
        equipCoordinate: undefined,        
        equipPosition: undefined,        
        equipDesc: undefined,        
        tenantId: undefined,        
        tenantName: undefined,        
        createdBy: undefined,        
        createdAt: undefined,        
        updatedBy: undefined,        
        updatedAt: undefined,        
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "序号不能为空", trigger: "blur" }
        ],        
        equipNum : [
            { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],        
        equipActiveStatus : [
            { required: true, message: "设备激活状态不能为空", trigger: "blur" }
        ],        
        equipName : [
            { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],        
        equipMac : [
            { required: true, message: "设备mac地址不能为空", trigger: "blur" }
        ],        
        equipIp : [
            { required: true, message: "设备IP地址不能为空", trigger: "blur" }
        ],        
        equipPort : [
            { required: true, message: "设备通信端口不能为空", trigger: "blur" }
        ],        
        protocolType : [
            { required: true, message: "协议类型不能为空", trigger: "blur" }
        ],        
        customerId : [
            { required: true, message: "客户编号不能为空", trigger: "blur" }
        ],        
        equipProvince : [
            { required: true, message: "省份不能为空", trigger: "blur" }
        ],        
        equipCity : [
            { required: true, message: "市不能为空", trigger: "blur" }
        ],        
        equipCounty : [
            { required: true, message: "区县不能为空", trigger: "blur" }
        ],        
        tenantName : [
            { required: true, message: "租户名称不能为空", trigger: "blur" }
        ],        
        createdBy : [
            { required: true, message: "创建人不能为空", trigger: "blur" }
        ],        
        createdAt : [
            { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = (row) => {
      resetForm();
      if(row) {
        getEquipInfo(row.customerId,row.equipNum).then((res)=>{
          const data = res.data;                   
          state.formData = data;
      })
    }
      state.isShowDialog = true;
    };
    const getCustomerList = () => {
      listCustomerInfo({pageNum:1,pageSize:1000}).then((res) => {
        state.customerList = lodash.get(res, 'data.list', []);
      });
    }
    watch(state.isShowDialog, (newVal) => {
      console.log("state.isShowDialog",state.isShowDialog);
        getCustomerList();
    }, { immediate: true });

    

    
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
      // 字典选项数据        
      const {
      equip_active_status,
      equip_protocol_type
    } = proxy.useDict('equip_active_status','equip_protocol_type')
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef);
      if (!formWrap) return;
      formWrap.validate((valid) => {
        console.log("state.formData",state.formData);
        if (!state.formData.equipProvince || !state.formData.equipCity || !state.formData.equipCounty) {
          ElMessage.warning('请选择省市区')
          return
        }
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addEquipInfo(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('equipInfoList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
          updateEquipInfo(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('equipInfoList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        equipNum: undefined,        
        equipType: undefined,        
        equipActiveStatus: 'waiting' ,        
        equipName: undefined,        
        equipMac: undefined,        
        equipIp: '127.0.0.1',        
        equipPort: undefined,        
        protocolType: undefined,        
        encryptKey: undefined,        
        customerId: undefined,        
        equipProvince: undefined,        
        equipCity: undefined,        
        equipCounty: undefined,        
        equipCoordinate: undefined,        
        equipPosition: undefined,        
        equipDesc: undefined,            
        createdBy: undefined,        
        createdAt: undefined,        
        updatedBy: undefined,        
        updatedAt: undefined,        
      }      
    };    
    return {
      equip_active_status,
      equip_protocol_type,
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,     
      getCustomerList, 
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
  .kv-label{margin-bottom: 15px;font-size: 14px;}
  .mini-btn i.el-icon{margin: unset;}
  .kv-row{margin-bottom: 12px;}
</style>